﻿Public Class frmCharOfAccount
    Dim dbBase As New dbGL
    Dim frmCtrl As New FormControl
    Dim Var As New varDeclare
    Dim TypeAct As String
    Dim dsBase As DataSet
    Dim dsGL As DataSet
    Dim EntryID As String
    Dim dtAllAccountDetail As DataTable
    Dim dtAccountType As DataTable

    Private Sub GenerateComboBox()
        cbGLAccountType.Items.Clear()
        dtAccountType = New dbSystem().FreeQueryDataTable("SELECT [TAB_TEXT], [TAB_VALUE] FROM TABLE_VALUE WHERE TAB_TYPE = 'GLAC_TYPE' ORDER BY TAB_SEQ")
        If (dtAccountType.Rows.Count > 0) Then
            For i = 0 To dtAccountType.Rows.Count - 1
                Try
                    cbGLAccountType.Items.Add(dtAccountType.Rows(i)("TAB_TEXT").ToString().Trim())
                Catch ex As Exception
                End Try
            Next
        End If
    End Sub

    Private Sub frmCharOfAccount_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        dsBase = dbBase.GLAccountList(Var.CompID)
        GenerateComboBox()
        dtAllAccountDetail = New dbSystem().FreeQueryDataTable("SELECT * FROM vw_ChartOfAccountDetail WHERE CompCOde = '" & Var.CompID & "'")
        If (dsBase.Tables(0).Rows.Count() > 0) Then
            dsGL = dbBase.ListMasterChartOfAccount("< 4")
            AdvTxtGLGroup.Visible = False
            AdvTxtGLGroup.Initialize(dsGL, "GL Account ID", txtGLGroupID)
            Dim dvGLGroup As DataView = New DataView(dsBase.Tables(0))
            dvGLGroup.RowFilter = "[GL Group] = [GL Account ID]"
            cbGLGroup.DataSource = dvGLGroup.Table.DefaultView.ToTable(True, "GL Group")
            cbGLGroup.DisplayMember = "GL Group"
            Textbox(False, False)
            ToolbarHeader1.Iniliaze("Entry : Chart of Account", Me, Nothing, GridAndFind1.getDataGridView(), "GL Account ID")
            GridAndFind1.Initialize(dsBase, "GL Account ID", ToolbarHeader1, Me, True)
            GridAndFind1.getDataGridView().Columns("GL Account Name").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
        End If
    End Sub

    Private Sub Textbox(st As Boolean, clear As Boolean)
        cbGLGroup.Enabled = st
        cbGLAccountType.Enabled = st
        cbGLAccountLevel.Enabled = st
        btnSearch.Enabled = st
        txtGLGroupID.Enabled = st
        txtGLGroupName.Enabled = st
        If (st) Then
            st = False
        Else : st = True
        End If
        txtGLAccountID.ReadOnly = st
        txtGLAccountID.BackColor = Color.White
        txtGLAccountName.ReadOnly = st
        txtGLAccountName.BackColor = Color.White
        GridAndFind1.Enabled = st
        If (clear) Then
            cbGLGroup.SelectedIndex = 0
            cbGLAccountType.SelectedIndex = 0
            cbGLAccountLevel.SelectedIndex = 0
            txtGLAccountID.Text = ""
            txtGLAccountName.Text = ""
            txtGLGroupID.Text = ""
            txtGLGroupName.Text = ""
        End If
    End Sub

    Private Sub tsNew_Click(ByVal sender As Object) Handles ToolbarHeader1.tsNewClick
        EntryID = ""
        TypeAct = "I"
        lblGroupType.Text = ""
        lblGLAccountType.Text = ""
        Textbox(True, True)
    End Sub

    Private Sub GridCellEnter_Click(ByVal sender As GridAndFind, KeyValue As String) Handles GridAndFind1.GridCellEnter
        Textbox(False, True)
        Dim dvFind As DataView = New DataView(dsBase.Tables(0))
        dvFind.RowFilter = "[GL Account ID] = '" & KeyValue & "'"
        EntryID = dvFind.Item(0).Row("GL Account ID").ToString()
        txtGLAccountID.Text = dvFind.Item(0).Row("GL Account ID").ToString()
        txtGLAccountName.Text = dvFind.Item(0).Row("GL Account Name").ToString()
        cbGLGroup.Text = dvFind.Item(0).Row("GL Group").ToString()
        txtGLGroupID.Text = dvFind.Item(0).Row("GL Group").ToString()
        cbGLAccountType.Text = dvFind.Item(0).Row("GL Type").ToString()
        cbGLAccountLevel.Text = dvFind.Item(0).Row("GL Level").ToString()

        Dim dvGroup As DataView = New DataView(dsGL.Tables(0))
        dvGroup.RowFilter = "[GL Account ID] = '" & txtGLGroupID.Text & "'"
        txtGLGroupName.Text = dvGroup.Item(0).Row("GL Account Name").ToString()
    End Sub

    Private Sub tsEdit_Click(ByVal sender As Object) Handles ToolbarHeader1.tsEditClick
        TypeAct = "U"
        Textbox(True, False)
        txtGLGroupID.Enabled = False
        txtGLGroupName.Enabled = False
        txtGLAccountID.ReadOnly = True
    End Sub

    Private Sub tsDelete_Click(ByVal sender As Object) Handles ToolbarHeader1.tsDeleteClick
        If (EntryID.Trim() <> "") Then
            TypeAct = "D"
            Dim result As String = dbBase.COAManagement(txtGLGroupID.Text, txtGLAccountID.Text, txtGLAccountName.Text, cbGLAccountType.Text, cbGLAccountLevel.Text, MDI.LoginUserName, TypeAct)
            If (result = "Success") Then
                frmCtrl.openChild(New frmCharOfAccount, Me.ParentForm)
            Else : MessageBox.Show(result)
            End If
        Else : MessageBox.Show("Please Choose User First")
        End If
    End Sub

    Private Sub tsPrint_Click(ByVal sender As Object) Handles ToolbarHeader1.tsPrintClick
        Dim frmPrint As New ViewReport
        Dim dgv As DataGridView = GridAndFind1.getDataGridView()
        If (dgv.Rows.Count > 0) Then
            Dim dsResult As New DataSet
            dsResult.Tables.Add("Result")
            dsResult.Tables("Result").Columns.Add("AccountGroup")
            dsResult.Tables("Result").Columns.Add("AccountID")
            dsResult.Tables("Result").Columns.Add("AccountName")
            For i = 0 To dgv.Rows.Count - 1
                Dim AccountGroup As String = dgv.Item("GL Group", i).Value
                Dim AccountID As String = dgv.Item("GL Account ID", i).Value
                Dim AccountName As String = dgv.Item("GL Account Name", i).Value
                dsResult.Tables("Result").Rows.Add(AccountGroup, AccountID, AccountName)
            Next
            frmPrint.dsReport = dsResult
            frmPrint.strReport = "COA"
            frmPrint.Show()
        End If
    End Sub

    Private Sub tsRefresh_Click(ByVal sender As Object) Handles ToolbarHeader1.tsRefreshClick
        frmCtrl.openChild(New frmCharOfAccount, Me.ParentForm)
    End Sub

    Private Sub tsCancel_Click(ByVal sender As Object) Handles ToolbarHeader1.tsCancelClick
        frmCtrl.openChild(New frmCharOfAccount, Me.ParentForm)
    End Sub

    Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
        Dim formFind As New frmFind
        formFind.lblTitle.Text = "Search : Chart of Account"
        formFind.QueryHeader = "SELECT DISTINCT GLId 'GL Account ID', GLName 'GL Account Name' FROM GLAccount WHERE GLGroup = GLId AND Comp = " + Var.CompID
        formFind.StartPosition = FormStartPosition.CenterScreen
        formFind.type = "cb"
        formFind.cbValue = cbGLGroup
        formFind.Show()
        MDI.Enabled = False
    End Sub

    Private Sub cbGLGroup_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbGLGroup.SelectedIndexChanged
        If (GridAndFind1.Enabled = False And cbGLGroup.Text <> "") Then
            txtGLAccountID.Text = dbBase.GenerateCOA(cbGLGroup.Text, Var.CompID)
        End If
    End Sub

    Private Sub ShowAdvTxtGLGroup_DoubleClick(sender As Object, e As EventArgs) Handles txtGLGroupName.DoubleClick, txtGLGroupID.DoubleClick
        AdvTxtGLGroup.Visible = True
        AdvTxtGLGroup.BringToFront()
        AdvTxtGLGroup._Show()
    End Sub

    Private Sub txtGLGroupID_TextChanged(sender As Object, e As EventArgs) Handles txtGLGroupID.TextChanged
        AdvTxtGLGroup.Visible = False
        If (txtGLGroupID.Text = "load") Then
            txtGLGroupID.Text = AdvTxtGLGroup.dsResult.Tables(0).Rows(0)("GL Account ID").ToString()
            txtGLGroupName.Text = AdvTxtGLGroup.dsResult.Tables(0).Rows(0)("GL Account Name").ToString()
        ElseIf (txtGLGroupID.Text.Length > 4) Then
            If (txtGLGroupID.Enabled = True And txtGLGroupID.Text <> "" And EntryID = "") Then
                txtGLAccountID.Text = dbBase.GenerateCOA(txtGLGroupID.Text, Var.CompID)
            End If
            Dim dv As DataView = New DataView(dtAllAccountDetail)
            dv.RowFilter = "[AccountCode] = '" & txtGLGroupID.Text & "'"
            Dim minLevel As Integer = CInt(dv.Item(0).Row("AccountLevel"))
            lblGroupType.Text = "[ " & dv.Item(0).Row("ReportTypeName").ToString() & " - " & dv.Item(0).Row("JournalTypeName").ToString() & " ]"
            cbGLAccountLevel.Items.Clear()
            If (txtGLGroupID.Text <> txtGLAccountID.Text) Then
                For i = minLevel + 1 To 4
                    cbGLAccountLevel.Items.Add(i)
                Next
            Else
                For i = minLevel To 4
                    cbGLAccountLevel.Items.Add(i)
                Next
            End If
        End If
    End Sub


    Private Sub tsSave_Click(ByVal sender As ToolbarHeader) Handles ToolbarHeader1.tsSaveClick
        If (cbGLAccountType.Text <> "" Or cbGLAccountLevel.Text <> "" Or txtGLAccountID.Text <> "" Or txtGLAccountName.Text <> "" Or txtGLGroupID.Text <> "" Or txtGLGroupName.Text <> "") Then
            Dim result As String = dbBase.COAManagement(txtGLGroupID.Text, txtGLAccountID.Text, txtGLAccountName.Text, cbGLAccountType.Text, cbGLAccountLevel.Text, MDI.LoginUserName, TypeAct)
            If (result = "Success") Then
                frmCtrl.openChild(New frmCharOfAccount, Me.ParentForm)
            Else : MessageBox.Show(result)
            End If
        Else
            MessageBox.Show("Please Complete all Field", "InComplete", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End If
    End Sub

    Private Sub cbGLAccountType_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbGLAccountType.SelectedIndexChanged
        If (cbGLAccountType.Text <> "") Then
            lblGLAccountType.Text = "- [" & dtAccountType.Rows(cbGLAccountType.SelectedIndex)("TAB_VALUE").ToString().Trim() & "]"
        Else : lblGLAccountType.Text = ""
        End If
    End Sub
End Class